package cn.com.yinhex.util;

import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

import ayou.util.DOC;
import ayou.util.StringUtil;

public class Ext {
	public static int setExtype(DOC extypes) {
		String[] keys = extypes.getKeys();
		int extype = 0;
		for (int i = 0; i < keys.length; i++) {
			if (extypes.geti(keys[i]) == 1) {
				extype += Config.extype.geti(keys[i]);
			}
		}
		return Integer.parseInt("" + extype, 2);
	}

	public static DOC getExtype(int extype) {
		String[] keys = Config.extype.getKeys();
		DOC output = new DOC();
		for (int i = 0; i < keys.length; i++) {
			if ((extype & (Config.extype.geti(keys[i]))) > 0) {
				output.put(keys[i], "1");
			}
		}
		return output;
	}

	public static DOC parseExt(DOC doc) {
		try {
			if (doc == null) {
				return null;
			}
			
			String ext = doc.get("ext");
			ext = ext.replaceAll("\r|\n", "");
			ext = ext.replaceAll("<\\/*[a-z]\\:[^>]+>", "");
			ext = ext.replaceAll("<\\?xml[^>]+>", "");
			if (!ext.startsWith("{")) {
				return doc;
			}
			JSONObject j = new JSONObject(ext);
			if (j != null) {
				for (Iterator iter = j.keys(); iter.hasNext();) {
					String key = (String) iter.next();
					doc.put(key, j.getString(key));
				}
			}
			int extypeb = doc.geti("extypeb");
			if (extypeb > 0) {
				DOC extypedoc = getExtype(extypeb);
				String[] extypekeys = extypedoc.getKeys();
				for (int i = 0; extypekeys != null && i < extypekeys.length; i++) {
					doc.put(extypekeys[i], extypedoc.get(extypekeys[i]));
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return doc;
	}

	public static DOC[] parseExt(DOC[] docs) {

		for (int i = 0; docs != null && i < docs.length; i++) {
			docs[i] = parseExt(docs[i]);
		}
		return docs;
	}

	public static String[] parseJsonArray(String str) {
		try {
			JSONArray j = new JSONArray(str);
			String[] ss = new String[j.length()];
			for (int i = 0; i < ss.length; i++) {
				ss[i] = (String) j.get(i);
			}
			return ss;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	public static DOC parseJson(String str) {
		if (str.indexOf("{") != 0) {
			return new DOC();
		}
		try {
			JSONObject j = new JSONObject(str);
			DOC doc = new DOC();
			for (Iterator it = j.keys(); it.hasNext();) {
				String key = (String) it.next();
				doc.put(key, j.getString(key));
			}
			return doc;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new DOC();
		}
	}

	public static String setJson(DOC ext) {
		JSONObject j = new JSONObject(ext);
		return j.toString();
	}

	public static String setJson(ArrayList list) {
		if (list == null) {
			return "";
		}
		JSONArray ja = new JSONArray();
		ja.put(list);
		return ja.toString();
	}

	public static String setJson(DOC[] docs) {
		if (docs == null) {
			return "";
		}
		JSONArray ja = new JSONArray();
		ja.put(docs);
		String output = ja.toString();
		output = StringUtil.replace(output, "[[{", "[{");
		output = StringUtil.replace(output, "}]]", "}]");
		return output;
	}

	public static int dropExtype(int extype, String type) {
		int e = Config.extype.geti(type);
		if ((extype & e) > 0) {
			extype = extype - e;
		}
		return Integer.parseInt("" + extype, 2);
	}

	public static int addExtype(int extype, String type) {
		int e = Config.extype.geti(type);
		if ((extype & e) <= 0) {
			extype = extype + e;
		}
		return Integer.parseInt("" + extype, 2);
	}

	public static void main(String[] args) {
		try {
			String json = "[\"5/f/2362752b1413cf7ffce9bdbcfb678bf5_0.jpg\",\"5/f/2362752b1413cf7ffce9bdbcfb678bf5_1.jpg\",\"5/f/2362752b1413cf7ffce9bdbcfb678bf5_2.jpg\",\"5/f/2362752b1413cf7ffce9bdbcfb678bf5_3.jpg\",\"5/f/2362752b1413cf7ffce9bdbcfb678bf5_4.jpg\"]";
			parseJsonArray(json);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
